package com.leha.web.controller;

import com.leha.core.entity.mybatis.LhBackendUser;
import com.leha.core.services.AdminServices;
import com.leha.web.controller.session.BackendSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.APIResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value="admin")
public class IndexController {
    @Autowired
    private HttpSession session;

    @Autowired
    private AdminServices adminServices;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("login")
    public String loginView() {
        return "login";
    }

    @RequestMapping("/logout")
    public String logoout() {
        BackendSession.removeSession(session);
        return "login";
    }

    @RequestMapping("/")
    public String indexView(Model model) {
        return "index";
    }

    @RequestMapping("dashboard")
    public String dashboardView(Model model) {
        return "dashboard";
    }

    @RequestMapping("/validate")
    @ResponseBody
    public APIResult loginValidate(@RequestParam(required = true) String username, @RequestParam(required = true) String password) {
        LhBackendUser user = adminServices.validate(username, password);
        if(user != null) {
            BackendSession.saveSession(session, user);
        }
        return new APIResult(user);
    }

    @RequestMapping("questions")
    public String questionView(Model model) {
        return "question/question";
    }

    @RequestMapping("imgs")
    public String imgsView(Model model) {
        return "question/imgs";
    }

    @RequestMapping("rewards")
    public String rewards(Model model) {
        return "rewards/request";
    }
    @RequestMapping("rewardItems")
    public String rewardItems(Model model) {
        return "rewards/items";
    }
}
